package com.tanhua.admin.module.login.controller;

import com.tanhua.admin.module.login.service.impl.UserServiceImpl;
import com.tanhua.admin.module.login.service.impl.VerificationServiceImpl;
import com.tanhua.admin.module.login.vo.AdminVo;
import com.tanhua.admin.module.login.vo.ErrorResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("system/users")
public class LoginController {

    @Autowired
    private VerificationServiceImpl verificationService;

    @Autowired
    private UserServiceImpl userService;


    /**
     *生成随机验证码图片
     * @param uuid 浏览器生成
     * @param response 响应
     */
    @GetMapping("verification")
    public void verification(@RequestParam("uuid")String uuid, HttpServletResponse response){
        //生成验证码
        String code = verificationService.getVerification(response);

        //调用verificationService中的saveVerification方法,传入uuid和验证码
        verificationService.saveVerification(uuid,code);
    }

    /**
     * 用户登录
     * @param param
     * @return
     */
    @PostMapping("login")
    public ResponseEntity<Object> login(@RequestBody Map<String,String> param){
        try {
            String username = param.get("username");
            String password = param.get("password");
            String verificationCode = param.get("verificationCode");
            String uuid = param.get("uuid");

            //返回token
            String token = userService.login(username,password,verificationCode,uuid);

            //判断token是否为空
            if (StringUtils.isNotEmpty(token)){
                Map<String,Object> map = new HashMap<>();
                map.put("token",token);

                return ResponseEntity.ok(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        ErrorResult.ErrorResultBuilder builder = ErrorResult.builder().errCode("000002").errMessage("登录失败！");
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(builder);
    }

    /**
     * 用户基本信息
     * @param token 用户token
     * @return
     */
    @PostMapping("profile")
    public ResponseEntity<AdminVo> profile(@RequestHeader("Authorization")String token){
        try {
            // 设置token,用空格替换bearer
            String newToken = token.replace("Bearer ","");
            AdminVo adminVo = userService.queryAdminByToken(newToken);
            if (adminVo != null){
                return ResponseEntity.ok(adminVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @PostMapping("logout")
    public void logout(@RequestHeader("Authorization")String token){
        // 设置token,用空格替换bearer
        String newToken = token.replace("Bearer ","");
        userService.logout(newToken);
    }
}
